from BaseViewTestCase import BaseViewTestCase
from skillchallenge.game import *
from skillchallenge.models import *
from django.db.models import Q

class EventPointsAndTimePassageTestCase(BaseViewTestCase):
    
    def setUp(self):
        self.game = Game()
        self.quest = Quest()
        self.char = Character.objects.create(name=Name.objects.create(text='name 1'), npc=False)
        self.char.train(Skill.objects.create(name='skill 1'))
        
        
    def test_criar_quest_quest_points_and_alike(self):
        # fazer ligacao entre usuario de personagem
        # fazer primeira quest singleplayer
        # boitar custo no path
        self.fail('implement')
    
    
    def test_event_availability(self):
        self.game.config.action_points_per_tick = 5
        self.game.config.save()
        
        player_event = self.quest.event_engine.add_event(player_will=True, online=True, description='test player avail 1',
                                                         actions = [self.quest.code_engine.create_message_action('test event avail 1.')],
                                                         info = 'player test 1', cost = 3)

        player_event2 = self.quest.event_engine.add_event(player_will=True, online=True, description='test player avail 2',
                                                         actions = [self.quest.code_engine.create_message_action('test event avail 2.')],
                                                         info = 'player test 2', cost = 3)

        
        self.game.tick()
        response = self.client.get('/game/' + str(self.char.id))
        
        self.assertTrue('Action Points: 5' in response.content)
        self.assertTrue(len(self.__event_options__(player_event, response.context[0].dicts[0]['options'])) > 0)
        self.assertTrue(len(self.__event_options__(player_event2, response.context[0].dicts[0]['options'])) > 0)
        
        self.game.player_fire(player_event, self.char)
        
        response = self.client.get('/game/' + str(self.char.id))
        self.assertTrue('Action Points: 2' in response.content)
        self.assertTrue(len(self.__event_options__(player_event2, response.context[0].dicts[0]['options'])) == 0)
        self.assertTrue(player_event2 in response.context[0].dicts[0]['expensive'])
    
    def test_action_points_replenish(self):
        self.game.config.action_points_per_tick = 5
        self.game.config.save()
        
        player_event = self.quest.event_engine.add_event(player_will=True, online=True, description='test player avail 1',
                                                         actions = [self.quest.code_engine.create_message_action('test event avail 1.')],
                                                         info = 'player test 1', cost = 3)

        self.game.tick()
        self.game.player_fire(player_event, self.char)
        self.game.tick()
        
        response = self.client.get('/game/' + str(self.char.id))
        self.assertTrue('Action Points: 5' in response.content)
    
    def est_tick_passage(self):
        from time import sleep
        self.game.config.seconds_per_tick = 2
        self.game.config.save()
        before = Tick.objects.latest()
        sleep(5)
        self.game.update_tick()
        after =  Tick.objects.latest()
        self.assertTrue(after.tick == before.tick + 2)
        
    def est_outdated_option(self):
        self.game.config.seconds_per_tick = 2
        self.game.config.save()
        self.game.tick()
        player_event = self.quest.event_engine.add_event(player_will=True, online=True, description='test player avail 1',
                                                         actions = [self.quest.code_engine.create_message_action('test event avail 1.')],
                                                         info = 'player test 1', cost = 3)
        response = self.client.get('/game/' + str(self.char.id))
        self.assertTrue(len(self.__event_options__(player_event, response.context[0].dicts[0]['options'])) > 0)
        opt = self.__event_options__(player_event, response.context[0].dicts[0]['options'])[0]
        from time import sleep
        sleep(5)
        
        response = self.client.get('/game/' + str(self.char.id) + '/option/' + str(opt.id))
                
        self.assertRedirects(response, '/error/1', 302)
